/******************************************************************************
 * @file     bsp_led_driver.c
 * @brief    led driver层
 * @author   Ruilist
 * @date     2025-08-13
 * @version  V1.0.0
 * @copyright Copyright (c) 2025
 *
 * @note     1 tab = 4 space
 *           依赖 bsp_led_driver.h
 *
 * @par 日志:
 * - V1.0.0 2025-08-13 Ruilist
 *****************************************************************************/

//******************************** Includes *********************************//

#include "bsp_led_driver.h"

//******************************** Includes *********************************//

//******************************** Defines **********************************//

//******************************** Defines **********************************//

//******************************** Declaring ********************************//

#ifndef OS_SUPPORTING
/**
 * @brief   led闪烁操作函数
 *
 * @param   bsp_led_driver_t * self     led实例结构体指针
 *
 * @return  led_status_t 函数运行状态
 *
 * @retval  LED_OK                操作成功
 * @retval  LED_ERROR_PARAMETER   传入参数错误
 */
led_status_t led_blink( bsp_led_driver_t *self )
{
    if ( NULL == self || NOT_INITED == self->init_status )
    {
#ifdef DEBUG
        DEBUG_OUT( "led_blink input parameter is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }
    uint32_t         cycle_local;
    uint32_t         blink_times_local;
    led_duty_cycle_t duty_cycle_local;
    uint32_t         led_toggle_time;

    cycle_local       = self->cycle;
    blink_times_local = self->blink_times;
    duty_cycle_local  = self->duty_cycle;

    if ( DUTY_CYCLE_1_1 == duty_cycle_local ||
         DUTY_CYCLE_1_2 == duty_cycle_local ||
         DUTY_CYCLE_1_3 == duty_cycle_local )
    {
        led_toggle_time = cycle_local / ( duty_cycle_local + 1 );
    }
    else
    {
#ifdef DEBUG
        DEBUG_OUT( "led_blink input duty_cycle_local is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }

    for ( uint32_t i = 0; i < blink_times_local; i++ )
    {
        for ( uint32_t j = 0; j < cycle_local; j++ )
        {
#ifdef OS_SUPPORTING
            self->p_os_delay->pf_os_delay( 1 );
#endif  // OS_SUPPORTING
            if ( j < led_toggle_time )
            {
                self->p_led_ops->pf_led_on();
            }
            else
            {
                self->p_led_ops->pf_led_off();
            }
        }
    }
    return LED_OK;
}
#endif  // !OS_SUPPORTING

#ifndef OS_SUPPORTING
/**
 * @brief   led实例传入led_controler参数函数
 *
 * @param   bsp_led_driver_t *const self     led实例结构体指针
 * @param   cycle                            闪烁周期(ms)
 * @param   blink_times                      闪烁次数
 * @param   duty_cycle                       闪烁占空比
 *
 * @return  led_status_t 函数运行状态
 *
 * @retval  LED_OK                操作成功
 * @retval  LED_ERROR_PARAMETER   传入参数错误
 */
static led_status_t led_controler( bsp_led_driver_t *const self,
                                   uint32_t                cycle,        // 闪烁周期(ms)
                                   uint32_t                blink_times,  // 闪烁次数
                                   led_duty_cycle_t        duty_cycle    // 闪烁占空比
)
{
    if ( NULL == self || NOT_INITED == self->init_status )
    {
#ifdef DEBUG
        DEBUG_OUT( "led_controler input parameter is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }

    // 判断数据合法性
    if ( !( ( cycle < 10000 ) &&
            ( blink_times < 1000 ) &&
            ( ( DUTY_CYCLE_1_1 <= duty_cycle ) && ( DUTY_CYCLE_1_3 >= duty_cycle ) ) ) )
    {
#ifdef DEBUG
        DEBUG_OUT( "led_controler input parameter is Illegal data\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }

    // 挂载led_controler的参数
    self->cycle       = cycle;
    self->blink_times = blink_times;
    self->duty_cycle  = duty_cycle;

    if ( LED_OK != led_blink( self ) )
    {
#ifdef DEBUG
        DEBUG_OUT( "led_controler call the led_blink is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }
    return LED_OK;
}
#endif  // !OS_SUPPORTING

/**
 * @brief   led实例初始化函数
 *
 * @param   bsp_led_driver_t *const self     led实例结构体指针
 *
 * @return  led_status_t 函数运行状态
 *
 * @retval  LED_OK                操作成功
 * @retval  LED_ERROR_PARAMETER   传入参数错误
 */
led_status_t led_driver_init( bsp_led_driver_t *const self )
{
    if ( NULL == self )
    {
#ifdef DEBUG
        DEBUG_OUT( "led inst input parameter is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }

    // 初始化led实例结构体的函数状态
    self->p_led_ops->pf_led_off();
    uint32_t time_temp = 0;
    self->p_time_base->pf_get_time( &time_temp );
#ifdef OS_SUPPORTING
    self->p_os_delay->pf_os_delay( 1 );
#endif  // OS_SUPPORTING

    return LED_OK;
}

/**
 * @brief   led实例挂载函数
 *
 * @param   bsp_led_driver_t *const self     led实例结构体指针
 * @param   led_operations_t *const led_ops  led亮灭操作函数指针
 * @param   time_base_t *const time_base     时基函数指针
 * @param   os_delay_t *const os_delay       OS延时函数指针
 *
 * @return  led_status_t 函数运行状态
 *
 * @retval  LED_OK                操作成功
 * @retval  LED_ERROR_PARAMETER   传入参数错误
 */
led_status_t led_driver_inst( bsp_led_driver_t *const self,
                              led_operations_t *const led_ops,
                              time_base_t *const      time_base,
#ifdef OS_SUPPORTING
                              os_delay_t *const os_delay
#endif  // OS_SUPPORTING
)
{
#ifdef DEBUG
    DEBUG_OUT( "led inst start \r\n" );
#endif  // DEBUG
    if ( NULL == self ||
         NULL == led_ops ||
         NULL == time_base ||
#ifdef OS_SUPPORTING
         NULL == os_delay
#endif  // OS_SUPPORTING
    )
    {
#ifdef DEBUG
        DEBUG_OUT( "led inst input parameter is error\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }
    if ( INITED == self->init_status )
    {
#ifdef DEBUG
        DEBUG_OUT( "led instance is inited\r\n" );
#endif  // DEBUG
        return LED_ERROR_PARAMETER;
    }

    // 挂载led实例结构体所需的函数
    self->p_led_ops   = led_ops;
    self->p_time_base = time_base;
#ifdef OS_SUPPORTING
    self->p_os_delay = os_delay;
#endif  // OS_SUPPORTING

#ifndef OS_SUPPORTING
    self->pf_led_controler = led_controler;
#endif  // !OS_SUPPORTING

    // 初始化led实例结构体的成员
    self->cycle       = 0;
    self->blink_times = 0;
    self->duty_cycle  = DUTY_CYCLE_x_x;

    // 初始化led实例
    if ( LED_OK != led_driver_init( self ) )
    {
#ifdef DEBUG
        DEBUG_OUT( "led driver init failed\r\n" );
#endif  // DEBUG
        self->p_led_ops   = NULL;
        self->p_time_base = NULL;
#ifdef OS_SUPPORTING
        self->p_os_delay = NULL;
#endif  // OS_SUPPORTING
        return LED_ERROR_PARAMETER;
    }

    // 把led实例的初始化状态置为已初始化
    self->init_status = INITED;
#ifdef DEBUG
    DEBUG_OUT( "led init successfully\r\n" );
#endif  // DEBUG
    return LED_OK;
}

//******************************** Declaring ********************************//
